﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace bomberman2passgen
{
    public partial class Form1 : Form
    {
        int baseValue = 0, mod1Value = 0, mod2Value = 0, numBomb = 0, bombSize = 0, area1 = 0, area2 = 0,
            asIndex = 0, checksum = 0, tempInt = 0, tempInt2 = 0;
        string passOrderString, tempString, baseValueS, mod1ValueS, mod2ValueS, numBombS, bombSizeS,
            area1S, area2S, checksumS;

        public Form1()
        {
            InitializeComponent();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            areaComboBox.Items.Add("1 - 1");
            areaComboBox.Items.Add("1 - 2");
            areaComboBox.Items.Add("1 - 3");
            areaComboBox.Items.Add("1 - 4");
            areaComboBox.Items.Add("1 - 5");
            areaComboBox.Items.Add("1 - 6");
            areaComboBox.Items.Add("1 - 7");
            areaComboBox.Items.Add("1 - 8");
            areaComboBox.Items.Add("2 - 1");
            areaComboBox.Items.Add("2 - 2");
            areaComboBox.Items.Add("2 - 3");
            areaComboBox.Items.Add("2 - 4");
            areaComboBox.Items.Add("2 - 5");
            areaComboBox.Items.Add("2 - 6");
            areaComboBox.Items.Add("2 - 7");
            areaComboBox.Items.Add("2 - 8");
            areaComboBox.Items.Add("3 - 1");
            areaComboBox.Items.Add("3 - 2");
            areaComboBox.Items.Add("3 - 3");
            areaComboBox.Items.Add("3 - 4");
            areaComboBox.Items.Add("3 - 5");
            areaComboBox.Items.Add("3 - 6");
            areaComboBox.Items.Add("3 - 7");
            areaComboBox.Items.Add("3 - 8");
            areaComboBox.Items.Add("4 - 1");
            areaComboBox.Items.Add("4 - 2");
            areaComboBox.Items.Add("4 - 3");
            areaComboBox.Items.Add("4 - 4");
            areaComboBox.Items.Add("4 - 5");
            areaComboBox.Items.Add("4 - 6");
            areaComboBox.Items.Add("4 - 7");
            areaComboBox.Items.Add("4 - 8");
            areaComboBox.Items.Add("5 - 1");
            areaComboBox.Items.Add("5 - 2");
            areaComboBox.Items.Add("5 - 3");
            areaComboBox.Items.Add("5 - 4");
            areaComboBox.Items.Add("5 - 5");
            areaComboBox.Items.Add("5 - 6");
            areaComboBox.Items.Add("5 - 7");
            areaComboBox.Items.Add("5 - 8");
            areaComboBox.Items.Add("6 - 1");
            areaComboBox.Items.Add("6 - 2");
            areaComboBox.Items.Add("6 - 3");
            areaComboBox.Items.Add("6 - 4");
            areaComboBox.Items.Add("6 - 5");
            areaComboBox.Items.Add("6 - 6");
            areaComboBox.Items.Add("6 - 7");
            areaComboBox.Items.Add("6 - 8");

            areaComboBox.SelectedIndex = 0;

            numBombsComboBox.Items.Add("1");
            numBombsComboBox.Items.Add("2");
            numBombsComboBox.Items.Add("3");
            numBombsComboBox.Items.Add("4");
            numBombsComboBox.Items.Add("5");
            numBombsComboBox.Items.Add("6");
            numBombsComboBox.Items.Add("7");
            numBombsComboBox.Items.Add("8");

            numBombsComboBox.SelectedIndex = 0;

            bombExSizeComboBox.Items.Add("1");
            bombExSizeComboBox.Items.Add("2");
            bombExSizeComboBox.Items.Add("3");
            bombExSizeComboBox.Items.Add("4");
            bombExSizeComboBox.Items.Add("5");

            bombExSizeComboBox.SelectedIndex = 0;

            baseNumComboBox.Items.Add("0");
            baseNumComboBox.Items.Add("1");
            baseNumComboBox.Items.Add("2");
            baseNumComboBox.Items.Add("3");
            baseNumComboBox.Items.Add("4");
            baseNumComboBox.Items.Add("5");
            baseNumComboBox.Items.Add("6");
            baseNumComboBox.Items.Add("7");
            baseNumComboBox.Items.Add("8");
            baseNumComboBox.Items.Add("9");
            baseNumComboBox.Items.Add("10");
            baseNumComboBox.Items.Add("11");
            baseNumComboBox.Items.Add("12");
            baseNumComboBox.Items.Add("13");
            baseNumComboBox.Items.Add("14");
            baseNumComboBox.Items.Add("15");

            baseNumComboBox.SelectedIndex = 0;

            cm1ComboBox.Items.Add("0");
            cm1ComboBox.Items.Add("1");
            cm1ComboBox.Items.Add("2");
            cm1ComboBox.Items.Add("3");
            cm1ComboBox.Items.Add("4");
            cm1ComboBox.Items.Add("5");
            cm1ComboBox.Items.Add("6");
            cm1ComboBox.Items.Add("7");
            cm1ComboBox.Items.Add("8");
            cm1ComboBox.Items.Add("9");
            cm1ComboBox.Items.Add("10");
            cm1ComboBox.Items.Add("11");
            cm1ComboBox.Items.Add("12");
            cm1ComboBox.Items.Add("13");
            cm1ComboBox.Items.Add("14");
            cm1ComboBox.Items.Add("15");

            cm1ComboBox.SelectedIndex = 0;

            cm2ComboBox.Items.Add("0");
            cm2ComboBox.Items.Add("1");
            cm2ComboBox.Items.Add("2");
            cm2ComboBox.Items.Add("3");
            cm2ComboBox.Items.Add("4");
            cm2ComboBox.Items.Add("5");
            cm2ComboBox.Items.Add("6");
            cm2ComboBox.Items.Add("7");
            cm2ComboBox.Items.Add("8");
            cm2ComboBox.Items.Add("9");
            cm2ComboBox.Items.Add("10");
            cm2ComboBox.Items.Add("11");
            cm2ComboBox.Items.Add("12");
            cm2ComboBox.Items.Add("13");
            cm2ComboBox.Items.Add("14");
            cm2ComboBox.Items.Add("15");

            cm2ComboBox.SelectedIndex = 0;

            countryComboBox.Items.Add("USA");
            countryComboBox.Items.Add("JAPAN");

            countryComboBox.SelectedIndex = 0;
        }

        private void genButton_Click(object sender, EventArgs e)
        {
            asIndex = areaComboBox.SelectedIndex;

            switch (asIndex)
            {
                case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
                    area1 = 1;
                    break;
                case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15:
                    area1 = 2;
                    break;
                case 16: case 17: case 18: case 19: case 20: case 21: case 22: case 23:
                    area1 = 3;
                    break;
                case 24: case 25: case 26: case 27: case 28: case 29: case 30: case 31:
                    area1 = 4;
                    break;
                case 32: case 33: case 34: case 35: case 36: case 37: case 38: case 39:
                    area1 = 5;
                    break;
                case 40: case 41: case 42: case 43: case 44: case 45: case 46: case 47:
                    area1 = 6;
                    break;
                default:
                    area1 = 0;
                    break;
            }

            switch (asIndex)
            {
                case 0: case 8: case 16: case 24: case 32: case 40:
                    area2 = 1;
                    break;
                case 1: case 9: case 17: case 25: case 33: case 41:
                    area2 = 2;
                    break;
                case 2: case 10: case 18: case 26: case 34: case 42:
                    area2 = 3;
                    break;
                case 3: case 11: case 19: case 27: case 35: case 43:
                    area2 = 4;
                    break;
                case 4: case 12: case 20: case 28: case 36: case 44:
                    area2 = 5;
                    break;
                case 5: case 13: case 21: case 29: case 37: case 45:
                    area2 = 6;
                    break;
                case 6: case 14: case 22: case 30: case 38: case 46:
                    area2 = 7;
                    break;
                case 7: case 15: case 23: case 31: case 39: case 47:
                    area2 = 8;
                    break;
                default:
                    area2 = 0;
                    break;
            }
            area1 = area1 - 1;
            area2 = area2 - 1;
            numBomb = int.Parse(numBombsComboBox.Text) - 1;
            bombSize = int.Parse(bombExSizeComboBox.Text) - 1;
            baseValue = int.Parse(baseNumComboBox.Text);
            mod1Value = int.Parse(cm1ComboBox.Text);
            mod2Value = int.Parse(cm2ComboBox.Text);

            //passwordTextBox.Text = area1.ToString() + area2.ToString() + numBomb.ToString()
            //    + bombSize.ToString() + mod1Value.ToString() + mod2Value.ToString();

            checksum = (area1 + area2 + numBomb + bombSize + mod1Value + mod2Value) % 16;

            area1 = area1 ^ baseValue;
            area2 = area2 ^ baseValue;
            numBomb = numBomb ^ baseValue;
            bombSize = bombSize ^ baseValue;
            mod1Value = mod1Value ^ baseValue;
            mod2Value = mod2Value ^ baseValue;
            checksum = checksum ^ baseValue;


            /*Convert
            from 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
            to  0  1  4  6 12 11  9  3 13 10  2 15  8  7 14  5
            */

            tempInt = area1;
            convertPass();
            area1 = convertPass();

            tempInt = area2;
            convertPass();
            area2 = convertPass();

            tempInt = numBomb;
            convertPass();
            numBomb = convertPass();

            tempInt = bombSize;
            convertPass();
            bombSize = convertPass();

            tempInt = mod1Value;
            convertPass();
            mod1Value = convertPass();

            tempInt = mod2Value;
            convertPass();
            mod2Value = convertPass();

            tempInt = checksum;
            convertPass();
            checksum = convertPass();

            tempInt = baseValue;
            convertPass();
            baseValue = convertPass();

            /*Encode
            Raw  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
            US:  1  2  3  4  5  6  7  8  B  C  D  F  G  H  J  K
            JP:  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P
            */

            tempInt2 = area1;
            encodePass();
            area1S = encodePass();

            tempInt2 = area2;
            encodePass();
            area2S = encodePass();

            tempInt2 = numBomb;
            encodePass();
            numBombS = encodePass();

            tempInt2 = bombSize;
            encodePass();
            bombSizeS = encodePass();

            tempInt2 = mod1Value;
            encodePass();
            mod1ValueS = encodePass();

            tempInt2 = mod2Value;
            encodePass();
            mod2ValueS = encodePass();

            tempInt2 = checksum;
            encodePass();
            checksumS = encodePass();

            tempInt2 = baseValue;
            encodePass();
            baseValueS = encodePass();



            //pass order
            passOrderString = baseValueS + area1S + area2S +
                checksumS + numBombS + mod1ValueS +
                bombSizeS + mod2ValueS;

            passwordTextBox.Text = passOrderString;
        }


        public int convertPass()
        {
            /*Convert Function
            */

            switch (tempInt)
            {
                case 0:
                    tempInt2 = 0;
                    break;
                case 1:
                    tempInt2 = 1;
                    break;
                case 2:
                    tempInt2 = 4;
                    break;
                case 3:
                    tempInt2 = 6;
                    break;
                case 4:
                    tempInt2 = 12;
                    break;
                case 5:
                    tempInt2 = 11;
                    break;
                case 6:
                    tempInt2 = 9;
                    break;
                case 7:
                    tempInt2 = 3;
                    break;
                case 8:
                    tempInt2 = 13;
                    break;
                case 9:
                    tempInt2 = 10;
                    break;
                case 10:
                    tempInt2 = 2;
                    break;
                case 11:
                    tempInt2 = 15;
                    break;
                case 12:
                    tempInt2 = 8;
                    break;
                case 13:
                    tempInt2 = 7;
                    break;
                case 14:
                    tempInt2 = 14;
                    break;
                case 15:
                    tempInt2 = 5;
                    break;
                default:
                    tempInt2 = 0;
                    break;
            }

            return tempInt2;
        }

        public string encodePass()
        {
            /*Encode function
            Raw  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
            US:  1  2  3  4  5  6  7  8  B  C  D  F  G  H  J  K
            JP:  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P
            */
            if (countryComboBox.SelectedIndex == 0)
            {

                switch (tempInt2)
                {
                    case 0:
                        tempString = "1";
                        break;
                    case 1:
                        tempString = "2";
                        break;
                    case 2:
                        tempString = "3";
                            break;
                    case 3:
                        tempString = "4";
                        break;
                    case 4:
                        tempString = "5";
                        break;
                    case 5:
                        tempString = "6";
                        break;
                    case 6:
                        tempString = "7";
                        break;
                    case 7:
                        tempString = "8";
                        break;
                    case 8:
                        tempString = "B";
                        break;
                    case 9:
                        tempString = "C";
                        break;
                    case 10:
                        tempString = "D";
                        break;
                    case 11:
                        tempString = "F";
                        break;
                    case 12:
                        tempString = "G";
                        break;
                    case 13:
                        tempString = "H";
                        break;
                    case 14:
                        tempString = "J";
                        break;
                    case 15:
                        tempString = "K";
                        break;

                }
            }
            else
            {

                switch (tempInt2)
                {
                    case 0:
                        tempString = "A";
                        break;
                    case 1:
                        tempString = "B";
                        break;
                    case 2:
                        tempString = "C";
                        break;
                    case 3:
                        tempString = "D";
                        break;
                    case 4:
                        tempString = "E";
                        break;
                    case 5:
                        tempString = "F";
                        break;
                    case 6:
                        tempString = "G";
                        break;
                    case 7:
                        tempString = "H";
                        break;
                    case 8:
                        tempString = "I";
                        break;
                    case 9:
                        tempString = "J";
                        break;
                    case 10:
                        tempString = "K";
                        break;
                    case 11:
                        tempString = "L";
                        break;
                    case 12:
                        tempString = "M";
                        break;
                    case 13:
                        tempString = "N";
                        break;
                    case 14:
                        tempString = "O";
                        break;
                    case 15:
                        tempString = "P";
                        break;
                }
            }
            return tempString;
        }

        private void generateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            genButton_Click(sender, e);
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox1 ab = new AboutBox1();

            ab.ShowDialog();
        }

        private void resetToolStripMenuItem_Click(object sender, EventArgs e)
        {
            areaComboBox.SelectedIndex = 0;
            baseNumComboBox.SelectedIndex = 0;
            numBombsComboBox.SelectedIndex = 0;
            cm1ComboBox.SelectedIndex = 0;
            bombExSizeComboBox.SelectedIndex = 0;
            cm2ComboBox.SelectedIndex = 0;
            countryComboBox.SelectedIndex = 0;
            passwordTextBox.Clear();
        }



    }

}
